package com.zymail.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyChannel;
import com.zymail.bean.ZyGood;
import com.zymail.bean.ZyGoodDe;
import com.zymail.bean.ZyGoodImg;
import com.zymail.service.ZyChannelService;
import com.zymail.service.ZyGoodDeService;
import com.zymail.service.ZyGoodImgService;
import com.zymail.service.ZyGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/util")
public class UtilController extends BaseController{

    @Autowired
    ZyChannelService channelService;

    @Autowired
    ZyGoodDeService goodDeService;

    @Autowired
    ZyGoodImgService imgService;

    /**
     * 得到首页的channel
     * @return  List<Zychannel>
     */
    @GetMapping("/mainChannel")
    public List<ZyChannel> get_main_channel(){
        QueryWrapper<ZyChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("role", 1);  //1代表首页channel
        return channelService.list(wrapper);
    }

    /**
     * 获取
     * @return  List<Zychannel>
     */
    @GetMapping("/all")
    public Map<String,Object> get_all_channel(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<ZyChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("role", 1);  //1代表首页channel
        List<ZyChannel> zychannels = channelService.list(wrapper);
        map.put("code",0);
        map.put("data",zychannels);
        return map;
    }

    /**
     * 通过类别id得到图片
     * @param deId  类别id
     * @return  图片类
     */
    @GetMapping("/getimgbydeid/{deId}")
    public ZyGoodImg getimgBydeId(@PathVariable("deId") Integer deId){
        ZyGoodDe de = goodDeService.getById(deId);

        QueryWrapper<ZyGoodImg> wrapper = new QueryWrapper<>();
        wrapper.eq("good_id", de.getGoodId());
        wrapper.eq("type", 1);
        return imgService.getOne(wrapper);
    }


    @PostMapping("/channel/add")
    public String channeladd(ZyChannel zyChannel){
        boolean i = channelService.save(zyChannel);
        if(i){
            return "succ";
        }
        return "fail";
    }
    @PostMapping("/channel/edit")
    public String channeledit(ZyChannel zyChannel){
        boolean i = channelService.saveOrUpdate(zyChannel);
        if(i){
            return "succ";
        }
        return "fail";
    }
    @PostMapping("/channel/del")
    public String channeldel(ZyChannel zyChannel){
        boolean i = channelService.removeById(zyChannel.getId());
        if(i){
            return "succ";
        }
        return "fail";
    }
    /**
     * 得到首页的channel
     * @return  List<Zychannel>
     */
    @GetMapping("/channel/id")
    public ZyChannel channel_id(Integer id){
        QueryWrapper<ZyChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return channelService.getOne(wrapper);
    }

    private String getContextPath(){
        String contextPath = this.getClass().getResource("/").toString().replace("/", "\\\\");
        contextPath = contextPath.substring(contextPath.indexOf("\\")+2,contextPath.length());
        return contextPath;
    }

    /**
     * 上传文件
     * @param file  需要上传的文件
     * @param model model
     * @param request   httprequest
     * @return  filepath
     */
    @PostMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = getContextPath().split("target")[0]+"file\\\\";
        //String filePath = "C:\\Users\\12785\\Desktop\\web\\zhuiyuan-mall\\file\\"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "/temp-rainy/" + fileName;
        model.addAttribute("filename", filename);
        return filename;
    }
}
